package com.hushow.demo;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        System.out.println("========");

        /**
         *  A
         *    A1 *
         *       A11 *
         *           A111*
         *           A112*
         *       A12
         *    A2 *
         *    A3 *
         *  B ×
         *    B1 ×
         *    B2 ×
         *    B3 ×
         *
         */

        Node a11chilren1 = new Node().setId("A111").setName("A111").setPid("A11").setStatus(true);
        Node a11chilren2 = new Node().setId("A112").setName("A112").setPid("A11").setStatus(true);

        Node a1chilren1 = new Node().setId("A11").setName("A11").setPid("A1").setChildren(Arrays.asList(a11chilren1, a11chilren2));

        Node a1chilren2 = new Node().setId("A12").setName("A12").setPid("A1").setStatus(true);
        Node achilren1 = new Node().setId("A1").setName("A1").setPid("A").setChildren(Arrays.asList(a1chilren1, a1chilren2));

        Node achilren2 = new Node().setId("A2").setName("A2").setPid("A").setStatus(true);;
        Node achilren3 = new Node().setId("A3").setName("A3").setPid("A").setStatus(true);
        Node aRoot = new Node().setId("A").setName("A").setPid("-1").setChildren(Arrays.asList(achilren1, achilren2, achilren3));


        Node bRoot = new Node().setId("B").setName("B").setPid("-1").setStatus(true).setStatus(true);

        Test t = new Test();
        for (Node root: Arrays.asList(bRoot)) {
            List<Node> nodeList =  new ArrayList<>();
            t.recC(root, nodeList);
            nodeList.forEach(c->{
                System.out.println(c.getId());
            });
        }
    }


    public boolean recC(Node node, List<Node> nodeList) {

        if(CollectionUtils.isEmpty(node.getChildren())){

            if("-1"==node.getPid() && node.getStatus()){
                nodeList.add(node);
                return false;
            }

            if(node.getStatus()){
                return true;
            }else{
                return false;
            }
        }

        Boolean isAllCheck = true;
        List<Node> tmpNodeList = new ArrayList<>();
        for(Node cNode : node.getChildren()) {
            Boolean cIsCheck = this.recC(cNode, nodeList);
            if(!cIsCheck){
                isAllCheck = false;
            }else{
                tmpNodeList.add(cNode);
            }
        }

        if(!isAllCheck){
            nodeList.addAll(tmpNodeList);
        }

        if(isAllCheck && node.getPid()=="-1"){
            nodeList.add(node);
        }
        return isAllCheck;
    }
}
